Imports System.Data.Linq.SqlClient
Imports System.Collections.Generic
Imports System.Collections.ObjectModel
Imports System.Collections.Specialized
Imports System.Runtime.Serialization
Imports System.ComponentModel
Imports ATS.Business.Entities.EntityStateClass



<DataContract(IsReference:=True)>
<KnownType(GetType(MainAssetsTable))>
Partial Public Class DisposedAsset
    Implements INotifyPropertyChanged, IEditableObject, IObjectWithChangeTracker
    Private DisposedAssetData As DisposedAsset


#Region "Page Load Events"
    'Public Sub New()
    '    AddHandler MainAssetsTable.AssociationChanged, AddressOf RoleChanged
    'End Sub
    'Private Sub RoleChanged(sender As Object, e As CollectionChangeEventArgs)
    '    ' Call the protected OnPropertyChanged method 
    '    ' which will fire the INotifyPropertyChanged.PropertyChanged event
    '    OnPropertyChanged("MainAssetsTable")
    'End Sub
#End Region
    'Public Event AssociationChanged As CollectionChangeEventHandler
#Region "Primitive Properties"
    <DataMember()>
    Public Overridable Property Assetno As String
        Get
            Return _assetno
        End Get
        Set(ByVal value As String)
            If Not Equals(_assetno, value) Then
                If MainAssetsTable IsNot Nothing AndAlso Not Equals(MainAssetsTable.Assetno, value) Then
                    MainAssetsTable = Nothing
                End If
                _assetno = value
            End If
        End Set
    End Property
    Private _assetno As String

    Private _DateSold As Nullable(Of Date)
    <DataMember()>
    Public Property DateSold() As Nullable(Of Date)
        Get
            Return _DateSold
        End Get

        Set(ByVal value As Nullable(Of Date))
            If _DateSold Is Nothing Then
                _DateSold = #12:00:00 AM#
            End If
            If value <> _DateSold Then
                Me.DataStateChanged( _
                EntityStateEnum.Modified, _
                "DateSold")
            End If
            _DateSold = value
            OnPropertyChanged("DateSold")
            OnPropertyChanged("AccumulatedDepreciation")
        End Set
    End Property

    Private _AmountSold As Nullable(Of Decimal)
    <DataMember()>
    Public Property AmountSold As Nullable(Of Decimal)
        Get
            Return _AmountSold
        End Get

        Set(ByVal value As Nullable(Of Decimal))
            If value <> _AmountSold Then
                Me.DataStateChanged( _
                EntityStateEnum.Modified, _
                "AmountSold")
            End If
            _AmountSold = value
            OnPropertyChanged("AmountSold")
        End Set
    End Property

    Private _Profit As Nullable(Of Decimal)
    <DataMember()>
    Public Property Profit As Nullable(Of Decimal)
        Get
            Return _Profit
        End Get

        Set(ByVal value As Nullable(Of Decimal))
            If value <> _Profit Then
                Me.DataStateChanged( _
                EntityStateEnum.Modified, _
                "Profit")
            End If
            _Profit = value
            OnPropertyChanged("Profit")
        End Set
    End Property

    Private _DisposalGainLoss As Nullable(Of Decimal)
    <DataMember()>
    Public Property DisposalGainLoss As Nullable(Of Decimal)
        Get
            Return _DisposalGainLoss
        End Get

        Set(ByVal value As Nullable(Of Decimal))
            If value <> _DisposalGainLoss Then
                Me.DataStateChanged( _
                EntityStateEnum.Modified, _
                "DisposalGainLoss")
            End If
            _DisposalGainLoss = value
            OnPropertyChanged("DisposalGainLoss")
        End Set
    End Property

    Private _AccumulatedDepreciation As Nullable(Of Decimal)
    Public ReadOnly Property AccumulatedDepreciation() As Nullable(Of Decimal)
        Get
            If Not Assetno Is Nothing Then
                Dim a As Decimal = _
                If(Not MainAssetsTable.DateAcquired Is Nothing, _
                   If(MainAssetsTable.Category.Years > 0 _
                      , ( _
                          ( _
                              If(MainAssetsTable.AssetAmount > 0 _
                                 , MainAssetsTable.AssetAmount _
                                 , MainAssetsTable.BaseCost + MainAssetsTable.VAT) / MainAssetsTable.Category.Years) / 12) * SqlMethods.DateDiffMonth(CDate(MainAssetsTable.DateAcquired), CDate(If(Not DateSold Is Nothing _
                                                                                                                                                                                                    , DateSold _
                                                                                                                                                                                                    , Now) _
                                                                                                                                                                                                ) _
                                                                                                                                                                                            ) _
                     , 0) _
                 , 0)
                Return a
            Else
                Return 0
            End If



        End Get

        'Set(value As Nullable(Of Decimal))
        '    _TotalCost = value
        'End Set
    End Property

    Private _DisposalAccmDepr As Nullable(Of Decimal)
    <DataMember()>
    Public Property DisposalAccmDepr As Nullable(Of Decimal)
        Get
            Return _DisposalAccmDepr
        End Get

        Set(ByVal value As Nullable(Of Decimal))
            If value <> _DisposalAccmDepr Then
                Me.DataStateChanged( _
                EntityStateEnum.Modified, _
                "DisposalAccmDepr")
            End If
            _DisposalAccmDepr = value
            OnPropertyChanged("DisposalAccmDepr")
        End Set
    End Property

    Private _DisposalBV As Nullable(Of Decimal)
    <DataMember()>
    Public Property DisposalBV As Nullable(Of Decimal)
        Get
            Return _DisposalBV
        End Get

        Set(ByVal value As Nullable(Of Decimal))
            If value <> _DisposalBV Then
                Me.DataStateChanged( _
                EntityStateEnum.Modified, _
                "DisposalBV")
            End If
            _DisposalBV = value
            OnPropertyChanged("DisposalBV")
        End Set
    End Property

    Private _DisposalMethod As String
    <DataMember()>
    Public Property DisposalMethod() As String
        Get
            Return _DisposalMethod
        End Get

        Set(ByVal value As String)
            If value <> _DisposalMethod Then
                Me.DataStateChanged( _
                EntityStateEnum.Modified, _
                "DisposalMethod")
            End If
            _DisposalMethod = value
            OnPropertyChanged("DisposalMethod")
        End Set
    End Property

    Private _isDirty As Boolean
    Public ReadOnly Property IsDirty() As Boolean
        Get
            Return Me.EntityState <> EntityStateEnum.Unchanged
        End Get
    End Property

#End Region
#Region "Navigation Properties"

    <DataMember()>
    Public Property MainAssetsTable() As MainAssetsTable
        Get
            Return _mainAssetsTable
        End Get
        Set(ByVal value As MainAssetsTable)
            If _mainAssetsTable IsNot value Then
                If ChangeTracker.ChangeTrackingEnabled AndAlso ChangeTracker.State <> ObjectState.Added AndAlso value IsNot Nothing Then
                    ' This the dependent end of an identifying relationship, so the principal end cannot be changed if it is already set,
                    ' otherwise it can only be set to an entity with a primary key that is the same value as the dependent's foreign key.
                    If Not Equals(Assetno, value.Assetno) Then
                        Throw New InvalidOperationException("The principal end of an identifying relationship can only be changed when the dependent end is in the Added state.")
                    End If
                End If
                Dim previousValue As MainAssetsTable = _mainAssetsTable
                _mainAssetsTable = value
                FixupMainAssetsTable(previousValue)
                OnNavigationPropertyChanged("MainAssetsTable")
            End If
        End Set
    End Property

    Private _mainAssetsTable As MainAssetsTable


#End Region
#Region "ChangeTracking"

    Protected Overridable Sub OnPropertyChanged(ByVal propertyName As String)
        If ChangeTracker.State <> ObjectState.Added AndAlso ChangeTracker.State <> ObjectState.Deleted Then
            ChangeTracker.State = ObjectState.Modified
        End If
        RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(propertyName))
    End Sub

    Protected Overridable Sub OnNavigationPropertyChanged(ByVal propertyName As String)
        RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(propertyName))
    End Sub

    Public Event PropertyChanged As PropertyChangedEventHandler Implements INotifyPropertyChanged.PropertyChanged
    Private _changeTracker As ObjectChangeTracker

    <DataMember()>
    Public Property ChangeTracker() As ObjectChangeTracker Implements IObjectWithChangeTracker.ChangeTracker
        Get
            If _changeTracker Is Nothing Then
                _changeTracker = New ObjectChangeTracker()
                AddHandler _changeTracker.ObjectStateChanging, AddressOf HandleObjectStateChanging
            End If
            Return _changeTracker
        End Get
        Set(ByVal value As ObjectChangeTracker)
            If _changeTracker IsNot Nothing Then
                RemoveHandler _changeTracker.ObjectStateChanging, AddressOf HandleObjectStateChanging
            End If
            _changeTracker = value
            If _changeTracker IsNot Nothing Then
                AddHandler _changeTracker.ObjectStateChanging, AddressOf HandleObjectStateChanging
            End If
        End Set
    End Property

    Private Sub HandleObjectStateChanging(ByVal sender As Object, ByVal e As ObjectStateChangingEventArgs)
        If e.NewState = ObjectState.Deleted Then
            Me.ClearNavigationProperties()
        End If
    End Sub

    Private _isDeserializing As Boolean
    Protected Property IsDeserializing() As Boolean
        Get
            Return _isDeserializing
        End Get
        Private Set(ByVal value As Boolean)
            _isDeserializing = value
        End Set
    End Property

    <OnDeserializing()>
    Public Sub OnDeserializingMethod(ByVal context As StreamingContext)
        IsDeserializing = True
    End Sub

    <OnDeserialized()>
    Public Sub OnDeserializedMethod(ByVal context As StreamingContext)
        IsDeserializing = False
        ChangeTracker.ChangeTrackingEnabled = True
    End Sub

    ' This entity type is the dependent end in at least one association that performs cascade deletes.
    ' This event handler will process notifications that occur when the principal end is deleted.
    Friend Sub HandleCascadeDelete(ByVal sender As Object, ByVal e As ObjectStateChangingEventArgs)
        If e.NewState = ObjectState.Deleted Then
            Me.MarkAsDeleted()
        End If
    End Sub

    Protected Overridable Sub ClearNavigationProperties()
        MainAssetsTable = Nothing
    End Sub

#End Region
#Region "Association Fixup"

    Private Sub FixupMainAssetsTable(ByVal previousValue As MainAssetsTable)
        If IsDeserializing Then
            Return
        End If

        If previousValue IsNot Nothing AndAlso previousValue.DisposedAsset Is Me Then
            previousValue.DisposedAsset = Nothing
        End If

        If MainAssetsTable IsNot Nothing Then
            MainAssetsTable.DisposedAsset = Me
            Assetno = MainAssetsTable.Assetno
        End If

        If ChangeTracker.ChangeTrackingEnabled Then
            If ChangeTracker.OriginalValues.ContainsKey("MainAssetsTable") AndAlso
                ChangeTracker.OriginalValues("MainAssetsTable") Is MainAssetsTable Then
                ChangeTracker.OriginalValues.Remove("MainAssetsTable")
            Else
                ChangeTracker.RecordOriginalValue("MainAssetsTable", previousValue)
            End If
            If MainAssetsTable IsNot Nothing AndAlso Not MainAssetsTable.ChangeTracker.ChangeTrackingEnabled Then
                MainAssetsTable.StartTracking()
            End If
        End If
    End Sub

#End Region

#Region "Support Procedures"
    Public Sub ResetDirtyBit()
        Me.EntityState = EntityStateEnum.Unchanged
    End Sub

    Public Sub BeginEdit() Implements System.ComponentModel.IEditableObject.BeginEdit
        DisposedAssetData = New DisposedAsset
        With DisposedAssetData
            .DateSold = DateSold
            .AmountSold = AmountSold
            .Profit = Profit
            .DisposalGainLoss = DisposalGainLoss
            .DisposalAccmDepr = DisposalAccmDepr
            .DisposalMethod = DisposalMethod
        End With
    End Sub

    Public Sub CancelEdit() Implements System.ComponentModel.IEditableObject.CancelEdit
        If Not DisposedAssetData Is Nothing Then
            With DisposedAssetData
                DateSold = .DateSold
                AmountSold = .AmountSold
                Profit = .Profit
                DisposalGainLoss = .DisposalGainLoss
                DisposalAccmDepr = .DisposalAccmDepr
                DisposalMethod = .DisposalMethod
            End With
        End If

    End Sub

    Public Sub EndEdit() Implements System.ComponentModel.IEditableObject.EndEdit
        DisposedAssetData = Nothing
    End Sub

    'Public Event PropertyChanged(sender As Object, e As System.ComponentModel.PropertyChangedEventArgs) Implements System.ComponentModel.INotifyPropertyChanged.PropertyChanged

    'Public Sub OnPropertyChanged(ByVal propertyName As String)
    '    RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(propertyName))
    'End Sub

    Private _EntityState As EntityStateEnum
    Public Property EntityState() As EntityStateEnum
        Get
            Return _EntityState
        End Get
        Private Set(ByVal value As EntityStateEnum)
            _EntityState = value
        End Set
    End Property

    Private Sub DataStateChanged(ByVal dataState As EntityStateEnum, ByVal propertyName As String)
        ' Raise the event
        If Not String.IsNullOrEmpty(propertyName) Then
            RaiseEvent PropertyChanged(Me, _
            New PropertyChangedEventArgs(propertyName))
        End If

        ' If the state is deleted, mark it as deleted
        If dataState = EntityStateEnum.Deleted Then
            Me.EntityState = dataState

        End If

        If Me.EntityState = _
                       EntityStateEnum.Unchanged Then
            Me.EntityState = dataState

        End If
    End Sub
#End Region
End Class
